package com.hln.jingwushi

import android.util.Log
import androidx.databinding.ViewDataBinding
import com.hln.jingwushi.utils.EnvironmentalCB
import com.hln.jingwushi.utils.EnvironmentalDetectionManager
import com.gdsz.libbasic.view.ui.BaseActivity
import com.zgkxzx.modbus4And.requset.ModbusParam
import com.zgkxzx.modbus4And.requset.ModbusReq
import com.zgkxzx.modbus4And.requset.OnRequestBack

abstract class MyBaseActivity<VB : ViewDataBinding> : BaseActivity<VB>() {


    fun modbusInit() {
        ModbusReq.getInstance().setParam(
            ModbusParam()
                .setHost("192.168.1.232")
                .setPort(1502)
                .setEncapsulated(false)
                .setKeepAlive(true)
                .setTimeout(2000)
                .setRetries(0)
        )
            .init(object : OnRequestBack<String?> {
                override fun onSuccess(s: String?) {
                    Log.d("asd", "onSuccess $s")
                    EnvironmentalDetectionManager.instance().startTask()

                }


                override fun onFailed(msg: String?) {
                    Log.d("asd", "onFailed $msg")
                }
            })
    }

    override fun onResume() {
        super.onResume()
        setModbusCB()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    private fun setModbusCB() {
        EnvironmentalDetectionManager.instance().setLister(object : EnvironmentalCB {
            override fun notify(values: ShortArray) {
                notifyData(values)
            }
        })
    }

    abstract fun notifyData (values: ShortArray)

}